<?php

namespace App\Http\Controllers\Statistical;

use App\Imports\Statistical;
use App\Http\Controllers\Controller;
use Maatwebsite\Excel\Facades\Excel;
use App\Repositories\Statistical\UserRepository;
use App\Http\Requests\Statistical\CreateUserRequest;
use App\Http\Requests\Statistical\UploadDataRequest;
use App\Repositories\Statistical\DayStatisticsRepository;

/**
 * Class UserController 用户相关
 * @package App\Http\Controllers\Statistical
 */
class UserController extends Controller
{
    /**
     * @var UserRepository
     */
    private $userRepository;
    /**
     * @var DayStatisticsRepository
     */
    private $dayStatisticsRepository;

    public function __construct(
        UserRepository $userRepository,
        DayStatisticsRepository $dayStatisticsRepository
    )
    {
        $this->userRepository          = $userRepository;
        $this->dayStatisticsRepository = $dayStatisticsRepository;
    }

    /**
     * 上传数据
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function upload()
    {
        return view('statistical.user.upload');
    }

    /**
     * 上传数据
     *
     * @param UploadDataRequest $request
     *
     * @return \Illuminate\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function uploadData(UploadDataRequest $request)
    {
        // 上传文件
        $path    = $request->file('file')->store('public/statistical/' . date('Ymd'));
        $dirname = storage_path('app/' . $path);
        $date    = $request->input('create_date');

        try {
            // 读取文件内容/
            $import = new Statistical();
            Excel::import($import, $dirname);

            // 添加或者修改数据
            $this->dayStatisticsRepository->updateOrCreate([
                'user_id'     => session('user.user_id'),
                'create_date' => $date,
            ], $import->toArray());

            return redirect('statistical/user/upload')->with('success', '上传成功');
        } catch (\Exception $e) {
            return redirect('statistical/user/upload')->with('error', $e->getMessage());
        }
    }

    /**
     * 添加子账号
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function create()
    {
        return view('statistical.user.create');
    }

    /**
     * 添加账号
     *
     * @param CreateUserRequest $request
     *
     * @return \Illuminate\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     * @throws \Littlebug\Repository\Exception
     */
    public function createUser(CreateUserRequest $request)
    {
        $this->userRepository->create(array_merge($request->all(), [
            'status'              => UserRepository::STATUS_ACTIVE,
            'type'                => UserRepository::TYPE_CHILD,
            'attribution_user_id' => session('user.user_id'),
        ]));

        return redirect('statistical/user/create')->with('success', '添加账号成功');
    }
}